package com.zjdiepu.www.proxy.biz.impl.transfer;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.proxy.model.ext.transfer.TransferYwBusinessBuyerExt;
import com.zjdiepu.www.proxy.dao.transfer.TransferYwBusinessBuyerMapper;
import com.zjdiepu.www.proxy.model.dto.transfer.TransferYwBusinessBuyer;
import com.zjdiepu.www.proxy.biz.transfer.ITransferYwBusinessBuyerService;

/**
 * 中转_yw_购车人信息Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年12月13日 17时50分<br/>
 * @since 1.0<br/>
 */
@Service
public class TransferYwBusinessBuyerServiceImpl extends BaseService implements ITransferYwBusinessBuyerService {
	public static final Logger logger = LoggerFactory.getLogger(TransferYwBusinessBuyerServiceImpl.class);

	@Autowired
	private TransferYwBusinessBuyerMapper transferYwBusinessBuyerMapper;
	
	@Override
	public void saveUpdateAndDelete(TransferYwBusinessBuyer transferYwBusinessBuyer, OperateType operType) {
		switch (operType) {
		case add:
			transferYwBusinessBuyer.initInsertData(transferYwBusinessBuyer.getModifier(), transferYwBusinessBuyer.getModifierName());
			transferYwBusinessBuyerMapper.insert(transferYwBusinessBuyer);
			break;
		case edit:
			transferYwBusinessBuyer.initUpdateData(transferYwBusinessBuyer.getModifier(), transferYwBusinessBuyer.getModifierName());
			// 影响的记录数
			int updateCount = transferYwBusinessBuyerMapper.updateByPrimaryKeySelective(transferYwBusinessBuyer);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			transferYwBusinessBuyer.initDeleteData(transferYwBusinessBuyer.getModifier(), transferYwBusinessBuyer.getModifierName());
			// 影响的记录数
			int deleteCount = transferYwBusinessBuyerMapper.updateByPrimaryKeySelective(transferYwBusinessBuyer);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<TransferYwBusinessBuyer> selectByExt(TransferYwBusinessBuyerExt transferYwBusinessBuyerExt) {
		return transferYwBusinessBuyerMapper.selectByExt(transferYwBusinessBuyerExt);
	}
	
	@Override
	public int countByExt(TransferYwBusinessBuyerExt transferYwBusinessBuyerExt){
		return transferYwBusinessBuyerMapper.countByExt(transferYwBusinessBuyerExt);
	}
	
	@Override
	public TransferYwBusinessBuyer selectById(String id) {
		return transferYwBusinessBuyerMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			transferYwBusinessBuyerMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(TransferYwBusinessBuyer transferYwBusinessBuyer) {
		return transferYwBusinessBuyerMapper.countRefined(transferYwBusinessBuyer);
	}

	@Override
	public TransferYwBusinessBuyer selectRefined(TransferYwBusinessBuyer transferYwBusinessBuyer) {
		return transferYwBusinessBuyerMapper.selectRefined(transferYwBusinessBuyer);
	}
	
	@Override
	public int deletedRefined(TransferYwBusinessBuyer transferYwBusinessBuyer) {
		return transferYwBusinessBuyerMapper.deletedRefined(transferYwBusinessBuyer);
	}

	@Override
	public boolean repeat(TransferYwBusinessBuyer transferYwBusinessBuyer) {
		int count = countRefined(transferYwBusinessBuyer);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(transferYwBusinessBuyer.getId())) {
			return true;
		}
		TransferYwBusinessBuyer dbItem = selectRefined(transferYwBusinessBuyer);
		if(null == dbItem) {
			return false;
		}
		return !transferYwBusinessBuyer.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return transferYwBusinessBuyerMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<TransferYwBusinessBuyer> transferYwBusinessBuyers) {
		return transferYwBusinessBuyerMapper.insertBatch(transferYwBusinessBuyers);
	}
}
